$(document).ready(function() {
    $(".fisarmonica").hide();

    $("#Reset").click(function() {
        $(".fisarmonica").fadeOut();
        $(":input", "#MapsForm").not(':button, :submit, :reset, :hidden').val('');
        $("#maps").html('');
    });

    $("#getmap").click(function() {
        // city must defined
        var input_address = jQuery.trim($("#address").val());
        if (input_address == '') {
            alert('Compile the field address!');
            return FALSE;
        }

        // geocoder
        var geocoder = new google.maps.Geocoder();

        geocoder.geocode({address: input_address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var lat = results[0].geometry.location.lat();
                var lng = results[0].geometry.location.lng();

                // set input form
                var components = results[0].address_components;
                $.each(components, function(index, value) {
                    if (value.types[0] != "undefined") {
                        if (value.types[0] == 'street') {
                            $("#street").val(value.long_name);
                        }
                        else if (value.types[0] == 'country') {
                            $("#nation").val(value.long_name);
                        }
                        else if (value.types[0] == 'administrative_area_level_1') {
                            $("#region").val(value.long_name);
                        }
                        else if (value.types[0] == 'administrative_area_level_2') {
                            $("#prov").val(value.short_name);
                        }
                        else if (value.types[0] == 'postal_code') {
                            $("#cap").val(value.long_name);
                        }
                        else if (value.types[0] == 'route') {
                            $("#street").val(value.long_name);
                        }
                        else if (value.types[0] == 'locality') {
                            $("#city").val(value.long_name);
                        }

                    }
                });
                $("#latitude").val(lat);
                $("#longitude").val(lng);
                // set img static maps
                $("#maps").html('<img src="https://maps.googleapis.com/maps/api/staticmap?center=' + lat + ',' + lng + '&zoom=15&size=300x150"/>');
                $(".fisarmonica").fadeIn('fast');
            }
            else {
                alert("Google Maps not found address!");
            }
        });
        return FALSE;
    });

});